package com.dcbeida.activity.zyff;


import android.app.Activity;
import android.content.DialogInterface;
import android.support.annotation.Nullable;

import com.dcbeida.activity.store.StoreContract;
import com.dcbeida.bean.CpreSendDrug;
import com.dcbeida.bean.DrugCode2Info;
import com.dcbeida.bean.DrugCodeInfo;
import com.dcbeida.bean.DrugInfo;
import com.dcbeida.bean.DrugInfoList;
import com.dcbeida.bean.PatientList;
import com.dcbeida.bean.Result_Api;
import com.dcbeida.bean.SendDrugList;
import com.dcbeida.bean.SourceList;
import com.dcbeida.callback.DialogCallback;
import com.dcbeida.callback.ErrorCallback;
import com.dcbeida.model.SotresalesModel;
import com.dcbeida.model.StoreModel;
import com.dcbeida.model.ZyFFModel;

import java.lang.reflect.Type;
import java.util.ArrayList;
import java.util.List;

import okhttp3.Call;
import okhttp3.Response;

/**
 * Created by hp on 2016/5/12.
 */
public class AlreadyDrugGivePresenter implements AlreadyDrugGiveContract.Present {


    private AlreadyDrugGiveContract.View view;


    private Activity act;


    private ZyFFModel model;

    private SotresalesModel sotresalesModel;

    private int hospitalid;

    public AlreadyDrugGivePresenter(AlreadyDrugGiveContract.View view, Activity act) {
        //我这里直接把activity作为view，所以不需要
        //mView.setPresenter(this);
        this.view = view;
        this.act = act;
        model = ZyFFModel.newInstance();
        sotresalesModel=SotresalesModel.newInstance();

    }

    public int getHospitalid() {
        return hospitalid;
    }

    public void setHospitalid(int hospitalid) {
        this.hospitalid = hospitalid;
    }

    @Override
    public void addbuydrugpresent(DrugCodeInfo drugCodeInfo, String codes, String originaldrugcodes) {
        model.addbuydrugpresent(drugCodeInfo, codes,
                originaldrugcodes, new AddbuydrugpresentCallBack(act, Result_Api.class, lis));
    }

    @Override
    public void getPatientInfo(int type, String keyword) {
        sotresalesModel.querypatient(type, keyword, new QuerypatientCallBack(act, PatientList.class,
                lis));
    }

    //根据条形码查询详细信息
    class AddbuydrugpresentCallBack<T> extends ErrorCallback<T> {

        public AddbuydrugpresentCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                view.showSuccess();

            }

        }


        @Override
        public void showError(String message) {
            view.showError(message);
        }

        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("数据未请求成功,请检查网络...");


        }
    }

    @Override
    public void start() {

    }

    //取消dialog 取消请求
    DialogInterface.OnCancelListener lis = new DialogInterface.OnCancelListener() {
        @Override
        public void onCancel(DialogInterface dialog) {
            model.getCancel();
        }
    };


    @Override
    public void queryCodeToDrugWithInfo(String drugcode) {
        model.querydrugcode(drugcode, new GetdrugcodeCallBack(act, DrugCodeInfo.class, lis));
    }

    @Override
    public void calcpresenddruglist(DrugCodeInfo drugCodeInfo, String codes) {

        model.calcpresenddruglist(drugCodeInfo, codes, new CalcpresenddruglistCallBack(act, SendDrugList.class, lis));
    }


    @Override
    public void querydruginfo(String drugcode) {
        model.querydruginfo(drugcode, new QuerydruginfoCallBack(act, DrugCode2Info.class, lis));
    }


    //根据条形码查询详细信息
    class QuerydruginfoCallBack<T> extends ErrorCallback<T> {

        public QuerydruginfoCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                DrugCode2Info info = (DrugCode2Info) data;
                view.showAddDrug(info);

            }

        }


        @Override
        public void showError(String message) {
            view.showError(message);
        }

        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("数据未请求成功,请检查网络...");


        }
    }


    //根据条形码查询详细信息
    class GetdrugcodeCallBack<T> extends ErrorCallback<T> {

        public GetdrugcodeCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                DrugCodeInfo info = (DrugCodeInfo) data;
                if (info.getHospitalid() != 0) {
                    hospitalid = info.getHospitalid();
                }
                view.showData(info);
            }

        }


        @Override
        public void showError(String message) {
            view.showError(message);
        }

        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("数据未请求成功,请检查网络...");


        }
    }


    class CalcpresenddruglistCallBack<T> extends DialogCallback<T> {

        public CalcpresenddruglistCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                SendDrugList sendDrugList = (SendDrugList) data;
                List<CpreSendDrug> list = (List<CpreSendDrug>) sendDrugList.getList();
                view.showListDrug(list);
            }
        }

        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("数据未请求成功,请检查网络...");


        }
    }


//    //药店销售-新增销售记录 返回结果
//    class ApiCallBack<T> extends DialogCallback<T> {
//
//        public ApiCallBack(Activity activity, Class<T> tclass, DialogInterface.OnCancelListener listener) {
//            super(activity, tclass, listener);
//        }
//
//        @Override
//        public void onResponse(T data) {
//            if (data != null) {
//                if (((Result_Api) data).getCode() == 1) {
//                    view.showTip(((Result_Api) data).getMsg());
//                    view.confimSuccess();
//                }
//            }
//        }
//
//
//        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
//            super.onError(call, response, e);
//            //view.showTip("数据未请求成功,请检查网络...");
//
//        }
//    }
//
//
//    @Override
//    public void getSourcelInfo(String keyword) {
//        model.getSourcelInfo(keyword, new AlreadyDrugGivePresenter.GetSourceInfoCallBack(act, SourceList.class, lis));
//    }
//
//    class GetSourceInfoCallBack<T> extends DialogCallback<T> {
//
//        public GetSourceInfoCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
//            super(activity, type, listener);
//        }
//
//        @Override
//        public void onResponse(T data) {
//            if (data != null) {
//                if (((SourceList) data).getList().size() >= 0) {
//                    view.showList(((SourceList) data).getList());
//                }
//            } else {
//                view.showList(null);
//            }
//        }
//
//
//        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
//            super.onError(call, response, e);
//            view.showList(null);
//
//        }
//    }

    //药店销售-查询患者信息列表
    class QuerypatientCallBack<T> extends DialogCallback<T> {

        public QuerypatientCallBack(Activity activity, Type type, DialogInterface.OnCancelListener listener) {
            super(activity, type, listener);
        }

        @Override
        public void onResponse(T data) {
            if (data != null) {
                if (data instanceof PatientList) {
                    if (((PatientList) data).getList().size() > 0) {
                        view.getPatientInfo(((PatientList) data).getList().get(0));
                    } else {
                        view.addPatient();
                    }
                }
            }
        }

        public void onError(boolean isFromCache, Call call, @Nullable Response response, @Nullable Exception e) {
            super.onError(call, response, e);
            view.showTip("数据未请求成功,请检查网络...");


        }
    }

}
